{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "from time import perf_counter"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "import neuralnetwork"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Unable to import cupy!\n",
      "Please DON'T use cupy associated objects or functions!\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "# start counting\r\n",
    "start = perf_counter()"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "# 训练数据集\r\n",
    "train_path = \"F:/Documents/mnist_datasets/train.csv\"\r\n",
    "# 测试数据集\r\n",
    "test_path = \"F:/Documents/mnist_datasets/test.csv\"\r\n",
    "# 保存权重矩阵的路径\r\n",
    "save_w_path = \"saved_data/\""
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "# number of input, hidden and output nodes\r\n",
    "input_nodes = 784\r\n",
    "hidden_nodes = 400\r\n",
    "output_nodes = 10\r\n",
    "\r\n",
    "# learning rate\r\n",
    "learning_rate = 0.01\r\n",
    "\r\n",
    "# create instance of neural network\r\n",
    "n = neuralnetwork.numpyNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "train_data_list = neuralnetwork.load_datasets(train_path)\r\n",
    "n = neuralnetwork.train_numpy(n, train_data_list)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Training...\n",
      "Epoch: 0\n",
      "Epoch: 1\n",
      "Epoch: 2\n",
      "Epoch: 3\n",
      "Epoch: 4\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "neuralnetwork.save_w_matrix(n.wih, n.who, path=save_w_path)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Saving Data...\n",
      "Saving Succeeded!\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "test_data_list = neuralnetwork.load_datasets(test_path)\r\n",
    "scorecard = neuralnetwork.test_network(n, test_data_list)\r\n",
    "performance = neuralnetwork.calculate_performance(scorecard)\r\n",
    "print(\"performance =\", performance)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Testing...\n",
      "performance = 0.73\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "# stop counting\r\n",
    "end = perf_counter()\r\n",
    "dur = end - start\r\n",
    "mins = (end - start) // 60\r\n",
    "secs = dur % 60\r\n",
    "print(\"I'v spent {:.00f} mins {:.00f} secs on this.\".format(mins, secs))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "I'v spent 0 mins 4 secs on this.\n"
     ]
    }
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.7.4",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.4 64-bit ('base': conda)"
  },
  "interpreter": {
   "hash": "a8f61be024eba58adef938c9aa1e29e02cb3dece83a5348b1a2dafd16a070453"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}